package com.salt.modules.sys.feign;

import com.google.common.collect.Lists;
import com.salt.common.config.properties.SaltProperites;
import com.salt.common.constant.Common;
import com.salt.common.utils.StringUtils;
import com.salt.core.web.BaseController;
import com.salt.modules.sys.entity.Office;
import com.salt.modules.sys.service.OfficeService;
import com.salt.modules.sys.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Description
 * @Author salt
 * @Date 2020/7/30 13:45
 * @Version 1.0
 */
@RestController
@RequestMapping(Common.Service.SALT_ADMIN_OFFICE_URL_PREFIX)
public class OfficeFeignController extends BaseController {

    @Autowired
    private OfficeService officeService;

    @PostMapping("getById")
    public Office get(@RequestParam(required = false) String id){
        if (StringUtils.isNotBlank(id)){
            return officeService.get(id);
        }else{
            return new Office();
        }
    }
    @PostMapping("findByIds")
    public List<Office> findByIds(@RequestParam("ids") List<String> ids) {
        return officeService.findByIds(ids);
    }

    @PostMapping("name")
    public Office getByOfficeName(@RequestParam("name") String name){
        return UserUtils.getByOfficeName(name);
    }

    @PostMapping("getChildren")
    public List<Office> getChildren(@RequestParam("parentId") String parentId){
        return officeService.getChildren(parentId);
    }

    @PostMapping("getUserableChildren")
    public List<Office> getUserableChildren(@RequestParam("parentId") String parentId) {
        return officeService.getChildren(parentId);
    }

    @GetMapping("findFuncLocationOfficeList")
    public List<Office> findFuncLocationOfficeList() {
        Office office = new Office();
        office.setFuncLocation("1");
        return officeService.findFuncLocationOfficeList(office);
    }

    @PostMapping("findList")
    public List<Office> findList(Office office) {
        return officeService.findList(office);
    }

    @PostMapping("findAllList")
    public List<Office> findAllList(Office office) {
        return officeService.findAllList(office);
    }

    @PostMapping("treeData")
    public List<Office> treeData(@RequestParam("id") String id) {
        List<Office> list = officeService.findList(true);
        List<Office> rootTree = getRootTree(list, id);
        return rootTree;
    }

    @PostMapping("treeDataIds")
    public List<String> treeDataIds(@RequestParam("id") String id) {
        List<Office> list = officeService.findList(true);
        List<Office> rootTree = getRootTree(list, id);
        ArrayList<String> ids = Lists.newArrayList();
        for (Office root:rootTree) {
            ids.add(root.getId());
            getChildOfTreeId(root, ids);
        }
        return ids;
    }

    @PostMapping("treeDataLocationIds")
    public List<String> treeDataLocationIds(@RequestParam("id") String id) {
        Office office = new Office();
        office.setFuncLocation("1");
        List<Office> list = officeService.findFuncLocationOfficeList(office);
        List<Office> rootTree = getLocationRootTree(list, id);
        ArrayList<String> ids = Lists.newArrayList();
        for (Office root:rootTree) {
            ids.add(root.getId());
            getChildOfTreeId(root, ids);
        }
        return ids;
    }

    /*public Boolean isSameCompany(Office office, Office company) {
        if (null == office)
            return false;
        if (null == company)
            company = UserUtils.getUser().getCompany();

    }*/

    private List<Office> getLocationRootTree(List<Office> list, String id) {
        List<Office> offices = Lists.newArrayList();
        List<Office> rootTrees = officeService.getChildren(id);
        for (Office root:rootTrees){
            if (SaltProperites.YES.equals(root.getUseable()) &&
                    Objects.equals("1", root.getFuncLocation())){
                offices.add(getChildOfTree(root, list));
            }
        }
        return offices;
    }

    private List<Office> getRootTree(List<Office> list, String id) {
        List<Office> offices = Lists.newArrayList();
        List<Office> rootTrees = officeService.getChildren(id);
        for (Office root:rootTrees){
            if (SaltProperites.YES.equals(root.getUseable())){
                offices.add(getChildOfTree(root, list));
            }
        }
        return offices;
    }

    private  Office getChildOfTree(Office officeItem,  List<Office> officeList) {
        officeItem.setChildren(Lists.newArrayList());
        for (Office child : officeList) {
            if (SaltProperites.YES.equals(child.getUseable())){
                if (child.getParentId().equals(officeItem.getId())) {
                    officeItem.getChildren().add(getChildOfTree(child, officeList));
                }
            }
        }
        return officeItem;
    }

    private List<String> getChildOfTreeId(Office office,  List<String> ids) {
        List<Office> officeList = office.getChildren();
        if (officeList != null && !officeList.isEmpty()) {
            for (Office child : officeList) {
                ids.add(child.getId());
                getChildOfTreeId(child, ids);
            }
        }
        return ids;
    }



}
